/**
 * 
 */
package com.manager;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.Util.LMSUtil;
import com.bean.LeaveDetailsBean;
import com.bean.LeaveInfoBean;
import com.dao.LeaveDetailsDAO;
import com.dao.LeaveInfoDAO;
import com.exception.LMSException;
import com.interfaces.LeaveManager;
import com.view.LeaveDetailView;
import com.view.PageDetailsView;

/**
 * The Class LeaveManagerImpl.
 * 
 * @author gbs02399
 */
public class LeaveManagerImpl implements LeaveManager {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.interfaces.LeaveManager#applyLeave(com.view.PageDetailsView)
	 */
	@Override
	public void applyLeave(final PageDetailsView pageView,LeaveDetailView leaveDetailView) throws LMSException {
		System.out.println(pageView.toString());
		Date fromDate = LMSUtil.getDate(pageView.getFromDate());
		Date toDate = LMSUtil.getDate(pageView.getToDate());
		LMSUtil.validateDate(pageView.getFromDate(), pageView.getToDate());
		Long noOfDays = (toDate.getTime() - fromDate.getTime()) / (1000 * 60 * 60 * 24);
		System.out.println(noOfDays);
		validateDays(leaveDetailView, noOfDays, pageView.getLeaveType());
		LeaveDetailsBean detailsBean = new LeaveDetailsBean();
		detailsBean.setLeaveId(Long.valueOf(leaveDetailView.getLeaveId()));
		detailsBean.setStartDate(fromDate);
		detailsBean.setEndDate(toDate);
		detailsBean.setNoOfDays(noOfDays);
		detailsBean.setReason(pageView.getLeaveReason());
		detailsBean.setState("Y");
		
		LeaveInfoBean infoBean = new LeaveInfoBean();
		infoBean.setLeaveId(Long.valueOf(leaveDetailView.getLeaveId()));
		if(pageView.getLeaveType().equals("CL")){
			infoBean.setLeaveBalance(Long.valueOf(leaveDetailView.getCasualLeaveCount())-noOfDays);
		}
		else if(pageView.getLeaveType().equals("EL")){
			infoBean.setLeaveBalance(Long.valueOf(leaveDetailView.getEarnedLeaveCount())-noOfDays);
		}
		else if(pageView.getLeaveType().equals("SL")){
			infoBean.setLeaveBalance(Long.valueOf(leaveDetailView.getSickLeaveCount())-noOfDays);
		}
		LeaveDetailsDAO.insertLeave(detailsBean, infoBean);
		
	}
	private void validateDays(LeaveDetailView view,Long noOfDays,String leaveType) throws LMSException{
		if(leaveType.equals("CL")){
			if(Long.valueOf(view.getCasualLeaveCount())<noOfDays){
				throw new LMSException("Sorry you dont have sufficient leaves");
			}
		}
		else if(leaveType.equals("EL")){
			if(Long.valueOf(view.getEarnedLeaveCount())<noOfDays){
				throw new LMSException("Sorry you dont have sufficient leaves");
			}
		}
		else if(leaveType.equals("SL")){
			if(Long.valueOf(view.getSickLeaveCount())<noOfDays){
				throw new LMSException("Sorry you dont have sufficient leaves");
			}
		}
	}

	public List<LeaveDetailsBean> getLeaveList (String empId,String leaveType) throws LMSException{
		List<LeaveDetailsBean> list = new ArrayList<LeaveDetailsBean>();
		LeaveInfoBean leaveInfoBean = LeaveInfoDAO.getLeaveInfoBean(Long.valueOf(empId),leaveType);
		list = LeaveDetailsDAO.listOfLeaves(leaveInfoBean.getLeaveId());
		return list;
	}
}
